package cn.com.oyat.idea.controller.virtual;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
public class DeviceDescriptionController {
    @Value("${server.port:8191}")
    private int serverPort;

    private final String deviceUuid = "uuid:" + UUID.randomUUID().toString();

    /**
     * 获取设备描述文档
     * @param request
     * @return
     */
    @GetMapping(value = {"/rootdesc.xml", "/description.xml", "/device.xml"},
            produces = MediaType.APPLICATION_XML_VALUE)
    public String getDeviceDescription(HttpServletRequest request) {
        String baseUrl = "http://" + request.getLocalAddr() + ":" + serverPort;

        System.out.println("📄 设备描述文档被请求: " + baseUrl);

        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n" +
                "  <specVersion>\n" +
                "    <major>1</major>\n" +
                "    <minor>0</minor>\n" +
                "  </specVersion>\n" +
                "  <device>\n" +
                "    <deviceType>urn:schemas-upnp-org:device:MediaRenderer:1</deviceType>\n" +
                "    <friendlyName>SpringBoot DLNA Receiver</friendlyName>\n" +
                "    <manufacturer>SpringBoot DLNA</manufacturer>\n" +
                "    <modelDescription>Virtual DLNA Media Renderer</modelDescription>\n" +
                "    <modelName>Virtual STB</modelName>\n" +
                "    <modelNumber>v1.0</modelNumber>\n" +
                "    <modelURL>" + baseUrl + "</modelURL>\n" +
                "    <UDN>" + deviceUuid + "</UDN>\n" +
                "    <serviceList>\n" +
                "      <service>\n" +
                "        <serviceType>urn:schemas-upnp-org:service:AVTransport:1</serviceType>\n" +
                "        <serviceId>urn:upnp-org:serviceId:AVTransport</serviceId>\n" +
                "        <SCPDURL>/upnp/services/avtransport/desc</SCPDURL>\n" +
                "        <controlURL>/upnp/services/avtransport/control</controlURL>\n" +
                "        <eventSubURL>/upnp/services/avtransport/event</eventSubURL>\n" +
                "      </service>\n" +
                "    </serviceList>\n" +
                "  </device>\n" +
                "</root>";
    }

    public String getDeviceUuid() {
        return deviceUuid;
    }
}